package jp.co.sapporobeer.FMCOutgoing;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Vibrator;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.util.Log;

public class Preferences extends PreferenceActivity {	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
        
        ListPreference list_preference = (ListPreference)getPreferenceScreen().findPreference("key_outgoing_special_no");
        list_preference.setSummary(list_preference.getEntry());
    }
    
    @Override  
    protected void onResume() {  
        super.onResume();  
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(listener);  
    }  
       
    @Override  
    protected void onPause() {  
        super.onPause();  
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);  
    }  
      
    private SharedPreferences.OnSharedPreferenceChangeListener listener =   
    	    new SharedPreferences.OnSharedPreferenceChangeListener() {  

        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,  String key) {

        	if (getString(R.string.pref_key_outgoing_special_no).equals(key)){
            	ListPreference list_preference = (ListPreference)getPreferenceScreen().findPreference("key_outgoing_special_no");
            	list_preference.setSummary(list_preference.getEntry());
        	}        	
        	
        	if (getString(R.string.pref_key_disconnect_end_vibrate).equals(key)){
    			Context context = getApplicationContext();
    			boolean cb_disconnect_end_vibrate = sharedPreferences.getBoolean("key_disconnect_end_vibrate", false);
    			
    			if (cb_disconnect_end_vibrate){
    				((Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(150);
    				
    				if (!isServiceRunning(context,PhoneStateService.class)){
    					context.startService(new Intent(context, PhoneStateService.class));
    				}
    			}        		
        	}
        }
    	    	
   	};

	private boolean isServiceRunning(Context c, Class<?> cls) {
   	    ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
   	    List<RunningServiceInfo> runningService = am.getRunningServices(Integer.MAX_VALUE);
   	    for (RunningServiceInfo i : runningService) {
   	        Log.d(getClass().getSimpleName(), "service: " + i.service.getClassName() + " : " + i.started);
   	        if (cls.getName().equals(i.service.getClassName())) {
   	            Log.d(getClass().getSimpleName(), "running");
   	            return true;
   	        }
   	    }
   	    return false;
   	}   	
}
